package com.softWang.system.controller;

import com.softWang.common.aop.annotation.SysOperationLog;
import com.softWang.common.response.PageBean;
import com.softWang.common.request.Idparam;
import com.softWang.common.response.ResponseResult;
import com.softWang.system.constants.SysModel;
import com.softWang.system.controller.vo.systemMenu.*;
import com.softWang.system.po.SystemMenu;
import com.softWang.system.service.SystemMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

@Validated
@RestController
@Api(value="菜单管理",tags="菜单管理")
@RequestMapping("/system/menu")
public class SystemMenuController {
    @Resource
    private SystemMenuService menuService;

    @PreAuthorize("@ss.hasPermi('system:menu:query')")
    @ApiOperation(value="菜单列表(分页)")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public ResponseResult<PageBean<SystemMenuQueryAllResult>> queryall(@RequestBody @Validated SystemMenuQueryAllParam param, HttpServletRequest request) {
        List<SystemMenuQueryAllResult> results=menuService.queryall(param);
        PageBean page=new PageBean(results);
        return ResponseResult.success(page);
    }

    @PreAuthorize("@ss.hasPermi('system:menu:add')")
    @ApiOperation(value="添加菜单")
    @SysOperationLog(value = "添加菜单", model = SysModel.MENUS_MANAGE)
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    //@Repeat(message = "重复提交")
    public ResponseResult<Long> add(@RequestBody @Validated SystemMenuAddParam param, HttpServletRequest request) throws Exception {
        SystemMenu menu = new SystemMenu();
        BeanUtils.copyProperties(param, menu);
        Long id = menuService.add(menu);
        return ResponseResult.success(id);
    }

    @PreAuthorize("@ss.hasPermi('system:menu:edit')")
    @ApiOperation(value="修改菜单")
    @SysOperationLog(value = "修改菜单", model = SysModel.MENUS_MANAGE)
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ResponseResult<Long> edit(@RequestBody @Validated SystemMenuEditParam param, HttpServletRequest request) {
        SystemMenu menu = new SystemMenu();
        BeanUtils.copyProperties(param, menu);
        Long id = menuService.edit(menu);
        return ResponseResult.success(id);
    }

    @PreAuthorize("@ss.hasPermi('system:menu:detail')")
    @ApiOperation(value="查看详情")
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public ResponseResult<SystemMenuDetailResult> detail(@RequestBody @Validated Idparam idparam) {        ;
        SystemMenuDetailResult result = new SystemMenuDetailResult();
        SystemMenu menu = menuService.getById(idparam.getId());
        BeanUtils.copyProperties(menu, result);
        return ResponseResult.success(result);
    }


    @PreAuthorize("@ss.hasPermi('system:menu:del')")
    @ApiOperation(value="删除")
    @SysOperationLog(value = "删除菜单", model = SysModel.MENUS_MANAGE)
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public ResponseResult del(@RequestBody @NotEmpty(message = "ids 不能为空") List<@NotNull(message = "ids 不能为空") Long> ids, HttpServletRequest request) {
        menuService.del(ids);
        return ResponseResult.success();
    }

    @PreAuthorize("@ss.hasPermi('system:menu:query')")
    @ApiOperation(value = "获取菜单树",notes = "获取菜单树")
    @RequestMapping(value = "/getMenuTree",method = RequestMethod.POST)
    public ResponseResult<List<SystemMenu>> getMenuTree(@RequestBody SystemMenuGetMenuTreeParam param, HttpServletRequest request) {
        List<SystemMenu> trees = menuService.getMenuTree(param);
        return ResponseResult.success(trees);
    }

}
